﻿using System;
using System.Collections.Generic;

using System.Web;
using com.byt.webcore;
using com.byt.suce.db.mapper;
using com.byt.webcore.utils;
using MYLSOFT.JsonSimple;
using com.byt.webcore.security;
using com.byt.suce.db.factory;
using com.byt.suce.db.dao;

namespace suce.security
{
    /// <summary>
    /// Summary description for Security
    /// </summary>
    public class Security : ServletBase
    {

        private IPkgSeguridad DAO = PkgSeguridadFactory.create();

        protected override void doCommand(ServletBase.Parameters p)
        {
            if (p.command.Equals("USER"))
                USER(p);
            else if (p.command.Equals("APP"))
                APP(p);
            else if (p.command.Equals("ROL"))
                ROL(p);
            else if (p.command.Equals("MENU"))
                MENU(p);
        }



        private void USER(ServletBase.Parameters p)
        {
            
            if (p.subcommand.Equals("GETLIST"))
            {
                PkgSeguridad.UserGetlistParam param = DAO.userGetlist(p.conn, p.getMap());
                p.print(Utils.getJSONArrayArray(param.CurOut));
                param.CurOut.Close();

            }
            
            else if (p.subcommand.Equals("GETBYID"))
            {
                PkgSeguridad.UserGetbyidParam param = DAO.userGetbyid(p.conn, p.getMap());
                JSONObject jo = new JSONObject();
                jo.Add("F", Utils.getJSONObject(param.CurForm));
                jo.Add("R", Utils.getJSONArrayArray(param.CurRol));
                param.CurForm.Close();
                param.CurRol.Close();

                p.print(jo);

            }
            
            else if (p.subcommand.Equals("UPDATE"))
            {
                String iduser = p.getParameter("iduser");
                PkgSeguridad.UserUpdateParam param = DAO.newUserUpdateParam(p.getMap());
                //param.setIdorgentidad(p.idorgentidad);
                DAO.userUpdate(p.conn, param);
                p.json.Add("iduser", param.Iduser);

                /*
                // si es usuario nuevo, setear el password por defecto
                if (string.IsNullOrEmpty(iduser))
                {
                    PasswordHasher.PasswordHash ph = PasswordHasher.hash(param.Vcnombre);
                    PkgSeguridad.UserChangepasswordParam paramSetPass = DAO.newUserChangepasswordParam(p.getMap());
                    paramSetPass.Iduser = param.Iduser;
                    paramSetPass.Vcpass = ph.getHashString();
                    paramSetPass.Vcpasssalt = ph.getSaltString();
                    DAO.userChangepassword(p.conn, paramSetPass);
                }
                */

            }
            else if (p.subcommand.Equals("DELETE"))
            {
                PkgSeguridad.UserDeleteParam param = DAO.newUserDeleteParam(p.getMap());
                DAO.userDelete(p.conn, param);

            }
            else if (p.subcommand.Equals("SETNEWPASS"))
            {
                PasswordHasher.PasswordHash ph = PasswordHasher.hash(p.getParameter("newpass"));
                PkgSeguridad.UserChangepasswordParam param = DAO.newUserChangepasswordParam(p.getMap());
                param.Vcpass = ph.getHashString();
                param.Vcpasssalt = ph.getSaltString();
                DAO.userChangepassword(p.conn, param);

            }
            else if (p.subcommand.Equals("CHANGEPASS"))
            {
                JSONObject user = com.byt.webcore.security.Security.loginUser(p);
                PasswordHasher.PasswordHash ph = PasswordHasher.hash(p.getParameter("newpass"));
                PkgSeguridad.UserChangepasswordParam param = DAO.newUserChangepasswordParam();
                param.Iduser = Utils.parseInteger(user["iduser"]);
                param.Vcpass = ph.getHashString();
                param.Vcpasssalt = ph.getSaltString();
                DAO.userChangepassword(p.conn, param);

            }
            else if (p.subcommand.Equals("GETLISTCOMBO"))
            {
                PkgSeguridad.UserGetlistComboParam param = DAO.userGetlistCombo(p.conn, p.getMap());
                p.print(Utils.getJSONArrayArray(param.CurOut));
                param.CurOut.Close();
            }
        }

        private void APP(ServletBase.Parameters p)
        {
            if (p.subcommand.Equals("GETLIST"))
            {

                PkgSeguridad.AppGetlistParam param = DAO.appGetlist(p.conn, p.getMap());
                p.print(Utils.getJSONArrayArray(param.CurOut));
                param.CurOut.Close();

            }
           else if (p.subcommand.Equals("GETBYID"))
            {

                PkgSeguridad.AppGetbyidParam param = DAO.appGetbyid(p.conn, p.getMap());
                JSONObject jo = Utils.getJSONObject(param.CurOut);
                param.CurOut.Close();
                p.print(jo);

            }
            else if (p.subcommand.Equals("UPDATE"))
            {

                PkgSeguridad.AppUpdateParam param = DAO.newAppUpdateParam(p.getMap());
                DAO.appUpdate(p.conn, param);
                p.json.Add("idapp", param.Idapp);

            }
             /*
            else if (p.subcommand.Equals("DELETE"))
            {

                DAO.appDelete(p.conn, p.getMap());

            }
             * */
        }

        private void ROL(ServletBase.Parameters p)
        {
            
            if (p.subcommand.Equals("GETLIST"))
            {
                PkgSeguridad.RolGetlistParam param = DAO.rolGetlist(p.conn, p.getMap());
                p.print(Utils.getJSONArrayArray(param.CurOut));
                param.CurOut.Close();

            }
            else if (p.subcommand.Equals("GETBYID"))
            {
                PkgSeguridad.RolGetbyidParam param = DAO.rolGetbyid(p.conn, p.getMap());
                JSONObject jo = new JSONObject();
                jo.Add("form", Utils.getJSONObject(param.CurForm));
                jo.Add("actividades", Utils.getJSONArrayArray(param.CurActividades));

                p.print(jo);
                param.CurForm.Close();
                param.CurActividades.Close();

            }
            else if (p.subcommand.Equals("UPDATE"))
            {
                PkgSeguridad.RolUpdateParam param = DAO.rolUpdate(p.conn, p.getMap());
                p.json.Add("idrol", param.Idrol);

            }
            else if (p.subcommand.Equals("DELETE"))
            {
                DAO.rolDelete(p.conn, p.getMap());

            }
        }

        private void MENU(ServletBase.Parameters p)
        {
            if (p.subcommand.Equals("GETTREE"))
            {
                MENU_GETTREE(p);

            }
            else if (p.subcommand.Equals("GETBYID"))
            {
                PkgSeguridad.MenuGetbyidParam param = DAO.menuGetbyid(p.conn, p.getMap());
                JSONObject jo = Utils.getJSONObject(param.CurOut);
                jo.Add("permisos", Utils.getJSONArrayArray(param.CurPermisos));
                p.print(jo);

                param.CurOut.Close();
                param.CurPermisos.Close();

            }
            else if (p.subcommand.Equals("GETROLES"))
            {
                PkgSeguridad.MenuGetrolesParam param = DAO.menuGetroles(p.conn, p.getMap());
                p.print(Utils.getJSONArrayArray(param.CurOut));
                param.CurOut.Close();

            }
            else if (p.subcommand.Equals("REGENERA"))
            {
                PkgSeguridad.MenuRegeneraParam param = DAO.newMenuRegeneraParam(p.getMap());
                DAO.menuRegenera(p.conn, param);

            }
            else if (p.subcommand.Equals("UPDATE"))
            {
                PkgSeguridad.MenuUpdateParam param = DAO.menuUpdate(p.conn, p.getMap());
                p.json.Add("idmenu", param.Idmenu);

            }
            else if (p.subcommand.Equals("DELETE"))
            {
                DAO.menuDelete(p.conn, p.getMap());

            }
            else if (p.subcommand.Equals("GETWINDOWBYUSER"))
            {
                //PkgSeguridad.MenuGetwindowByuserParam param = DAO.menuGetwindowByuser(p.conn, p.getMap());
                //WindowSecurity ws = new WindowSecurity(param.CurOut);
                //p.print(ws);
                //param.CurOut.Close();

            }
        }

        private void MENU_GETTREE(ServletBase.Parameters p)
        {
            PkgSeguridad.MenuGettreeParam param = DAO.newMenuGettreeParam(p.getMap());
            int? idapp = Utils.parseInteger(p.getParameter("idapp"));

            if (idapp != null)
                param.Idmenusup = idapp;

            DAO.menuGettree(p.conn, param);
            p.print(Utils.getJSONArrayObject(param.CurOut));
            param.CurOut.Close();
        }

    }
}